
package finalproject.client;

import java.util.List;

import finalproject.shared.Student;
import finalproject.shared.Vak;
import finalproject.shared.Absentie;
import finalproject.shared.Persoon;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.HTMLTable.Cell;


public class AbsentieGui {
    
    private static final String LISTING_ROOT_PANEL = "guiListing";
    private static final String FORM_ROOT_PANEL = "guiForm";
    private static final String TOOL_BAR_ROOT_PANEL = "guiToolBar";
    private static final String FLEX_TABLE_ROOT_PANEL = "guiOverzicht";
    private static final int EDIT_LINK = 5;
    private static final int REMOVE_LINK = 6;
   
    protected Button verwijderflexbutton;
    protected Button ovokButton;
    protected Button overzichtButton;
    protected Button addButton;
    protected Button updateButton;
    protected Button addNewButton;
    protected ListBox studentNrField;
    protected ListBox vakNrField;
    protected TextBox naamField;
    protected TextBox datumField;
    protected TextBox redenField;
    protected Label status;
    protected FlexTable AbsentieFlexTable ;
    protected Grid listGrid;
    protected Grid formGrid;
    
  
    private List<Student> studenten;
    
    private List<Vak> vakken;
    
    private List<Absentie> absenties;
    private Absentie currentAbsentie;
    protected AbsentieClass absentieService;
        
    
    public void go() {
    	verwijderflexbutton= new Button ( "X" );
    	ovokButton = new Button ("ok en terugkeren");
    	overzichtButton = new Button("overzicht ophalen");
        addButton = new Button("Bevestigen");
        addNewButton = new Button("Aanmelden voor absentie");
        updateButton = new Button("Update Absentie");
        studentNrField = new ListBox();
        vakNrField = new ListBox();
        naamField = new TextBox();
        datumField = new TextBox();
        redenField = new TextBox();
       listGrid = new Grid(6,10);
       AbsentieFlexTable = new FlexTable();
      
       
       
       //flextable
       AbsentieFlexTable.setText(0, 0, "Persoon");
       AbsentieFlexTable.setText(0, 1, "Vak");
       AbsentieFlexTable.setText(0, 2, "Datum");
       AbsentieFlexTable.setText(0, 3, "Reden");
	    
       AbsentieFlexTable.setCellPadding(6);
	    
      AbsentieFlexTable.getRowFormatter().addStyleName(0, "absentieListHeader");
      AbsentieFlexTable.addStyleName("absentieList");
       AbsentieFlexTable.getCellFormatter().addStyleName(0, 0, "absentieColumn0");
       //AbsentieFlexTable.getCellFormatter().addStyleName(0, 1, "absentieColumn1");
       AbsentieFlexTable.getCellFormatter().addStyleName(0, 2, "absentieColumn2");
       //AbsentieFlexTable.getCellFormatter().addStyleName(0, 3, "absentieColumn3");
       
        buildForm();
        placeWidgets();
    }

    //events aanroepen van clickhandlers
    public void wireEvents() {
    	listGrid.addClickHandler(new ClickHandler(){
            public void onClick(ClickEvent event) {
                 Cell cellForEvent = listGrid.getCellForEvent(event);
                 gui_eventListGridClicked(cellForEvent);                
            }});
    	
    	overzichtButton.addClickHandler(new ClickHandler() {
    		@Override
    		public void onClick(ClickEvent event){
    			gui_eventOverzichtButtonClicked();
    		}});
        
    	ovokButton.addClickHandler(new ClickHandler() {
    	@Override
    	public void onClick (ClickEvent event){
    		gui_eventOvokButtonClicked();
    	}});
    	
    	addButton.addClickHandler(new ClickHandler(){
            public void onClick(ClickEvent event) {
            	gui_eventAddButtonClicked();
            }});

    	updateButton.addClickHandler(new ClickHandler(){
            public void onClick(ClickEvent event) {
            	gui_eventUpdateButtonClicked();
            }});
        
    	addNewButton.addClickHandler(new ClickHandler(){
            public void onClick(ClickEvent event) {
            	gui_eventAddNewButtonClicked();
           }});
    }    
    
  
    private void buildForm() {
        formGrid = new Grid(5,3);
        formGrid.setVisible(false);

        formGrid.setWidget(0, 0, new Label("Student"));
        formGrid.setWidget(0, 1, studentNrField);        
        
        formGrid.setWidget(1, 0, new Label("Vak"));
        formGrid.setWidget(1, 1, vakNrField);
        
        formGrid.setWidget(2, 0, new Label("Datum"));
        formGrid.setWidget(2, 1, datumField);

        formGrid.setWidget(3, 0, new Label("Reden"));
        formGrid.setWidget(3, 1, redenField);
        
        formGrid.setWidget(4, 0, updateButton);
        formGrid.setWidget(4, 1, addButton);
        
    }

   
    private void placeWidgets() {
        RootPanel.get(LISTING_ROOT_PANEL).add(listGrid);
        RootPanel.get(FORM_ROOT_PANEL).add(formGrid);
        RootPanel.get(TOOL_BAR_ROOT_PANEL).add(addNewButton);
        RootPanel.get(TOOL_BAR_ROOT_PANEL).add(overzichtButton);
        RootPanel.get(TOOL_BAR_ROOT_PANEL).add(ovokButton);
        RootPanel.get(FLEX_TABLE_ROOT_PANEL).add(AbsentieFlexTable);
    }

    
    public void hideGui() {
    	formGrid.setVisible(false);
    	listGrid.setVisible(false);
    	addNewButton.setVisible(false);
    	 overzichtButton.setVisible(false);
    	 ovokButton.setVisible(false);
    	 AbsentieFlexTable.setVisible(false);
    }

    
   
    private void loadForm(Absentie absentie) {
        this.formGrid.setVisible(true);
        currentAbsentie = absentie;
        
        this.studentNrField.clear();
        int index = 0;
        int selected = 0;
        for (Student student : studenten) {
        	this.studentNrField.addItem(student.getPersoon().getNaam());
        	if (absentie.getStudentNr().longValue() == 
        			student.getStudentNr().longValue()) {
        		selected = index;
        	
        	}
        	index++;
        }        
        this.studentNrField.setSelectedIndex(selected);
        
        this.vakNrField.clear();
        index = 0;
        selected = 0;
        for (Vak vak : vakken) {
        	this.vakNrField.addItem(vak.getNaam());
        	if (absentie.getVakNr().longValue() == 
        			vak.getVakNr().longValue()) {
        		selected = index;
        		
        	}
        	index++;
        }        
        this.vakNrField.setSelectedIndex(selected);
        
        this.datumField.setText(absentie.getDatum());
        this.redenField.setText(absentie.getReden());
    }


    
    private void copyFieldDateToAbsentie() {
    	
    	int selected = this.studentNrField.getSelectedIndex();

    	Long studentNr = 0L;
        if (studenten.size() > selected) {
        	studentNr = studenten.get(selected).getStudentNr();
        }
        currentAbsentie.setStudentNr(studentNr);

    	selected = this.vakNrField.getSelectedIndex();

    	Long vakNr = 0L;
        if (vakken.size() > selected) {
        	vakNr = vakken.get(selected).getVakNr();
        }
        currentAbsentie.setVakNr(vakNr);

        currentAbsentie.setDatum(datumField.getText());
        currentAbsentie.setReden(redenField.getText());
    }

 
    public void gui_eventListGridClicked(Cell cellClicked) {
         int row = cellClicked.getRowIndex();
         int col = cellClicked.getCellIndex();
         
        
         Absentie absentie = this.absenties.get(row);
         
        
         if (col==EDIT_LINK) {
             this.addNewButton.setVisible(false);
             this.updateButton.setVisible(true);
             this.addButton.setVisible(false);
             this.overzichtButton.setVisible(false);
             this.ovokButton.setVisible(false);
             loadForm(absentie);
         } else if (col==REMOVE_LINK) {
        	 if (Window.confirm("Verwijderen deelname ?")) {
        		 this.absentieService.removeAbsentie(absentie);
        	 }
         }
    }

    public void gui_eventabsentieFlextableClicked(Cell cellClicked) {
        int row = cellClicked.getRowIndex();
        int col = cellClicked.getCellIndex();
        
       
        Absentie absentie = this.absenties.get(row);
        
       
        if (col==EDIT_LINK) {
            this.addNewButton.setVisible(false);
            this.updateButton.setVisible(true);
            this.addButton.setVisible(false);
            this.overzichtButton.setVisible(false);
            this.ovokButton.setVisible(false);
            loadForm(absentie);
        } else if (col==REMOVE_LINK) {
       	 if (Window.confirm("Verwijderen deelname ?")) {
       		 this.absentieService.removeAbsentie(absentie);
       	 }
        }
   }

   
    public void gui_eventAddButtonClicked() {
        addNewButton.setVisible(true);
        formGrid.setVisible(false);
        copyFieldDateToAbsentie();
        this.absentieService.addAbsentie(currentAbsentie);
    }
//bookmark/task:D
    public void gui_eventOverzichtButtonClicked() {
    	addNewButton.setVisible(false);
    	formGrid.setVisible(false);
    	ovokButton.setVisible(true);
    	overzichtButton.setVisible(false);
    	AbsentieFlexTable.setVisible(true);
    	//onderstaande methode is om het lijstje met info uit de datastore met edit en delete weg
    	//te halen omdat dit nogal onzinnig is om er nog eens onder te zetten;)
    	listGrid.setVisible(false);
    	//haalOverzichtop();
    	/*bovenstaande methode bestaat nog niet dus ff slashen miss handig om bij 
    	public void service_absentiesListRetrievedFromService(List<Absentie> result) {
    	te kijken op regel ~276
    	
    	*/
    }
    
    public void gui_eventOvokButtonClicked(){
    	hideGui();
    }
    
    public void gui_eventUpdateButtonClicked() {
        formGrid.setVisible(false);
        copyFieldDateToAbsentie();
        this.absentieService.updateAbsentie(currentAbsentie);
    }

    
    public void gui_eventAddNewButtonClicked() {
        this.addNewButton.setVisible(false);
        this.updateButton.setVisible(false);
        this.addButton.setVisible(true);
        Absentie absentie = new Absentie();
        absentie.setStudentNr(0L);
        absentie.setVakNr(0L);
        loadForm(absentie);
    }

   
    public void service_studentenListRetrievedFromService(List<Student> result) {
    	this.studenten = result;
    	int row = 1;
        
        for (Student student : result) {
        	
            this.AbsentieFlexTable.setWidget(row, 0, new Label (student.getPersoon().getNaam()));
            row ++;
        }
    }
  	
    
 
    public void service_vakkenListRetrievedFromService(List<Vak> result) {
    	this.vakken = result;	
    	int row = 1;
        
        for (Vak vak : result) {
        	
            this.AbsentieFlexTable.setWidget(row, 1, new Label (vak.getNaam()));
            this.AbsentieFlexTable.setWidget(row,   EDIT_LINK, new Anchor("Edit"));
            this.AbsentieFlexTable.setWidget(row,   REMOVE_LINK, new Anchor("Remove"));
            row ++;
        }
    }
    

    public void service_absentiesListRetrievedFromService(List<Absentie> result) {

        
    	listGrid.setVisible(false);
    	addNewButton.setVisible(true);
        overzichtButton.setVisible(true);
    	
        this.absenties = result;
        this.listGrid.clear();
        this.listGrid.resizeRows(this.absenties.size());
        int row = 1;
        this.addNewButton.setVisible(true);
        
        for (Absentie absentie : result) {
        	
            this.listGrid.setWidget(row, 1, new Label (absentie.getDatum()));
            this.listGrid.setWidget(row, 2, new Label (absentie.getReden()));
            this.listGrid.setWidget(row, EDIT_LINK, new Anchor("Edit"));
            this.listGrid.setWidget(row,   REMOVE_LINK, new Anchor("Remove"));
            this.AbsentieFlexTable.setWidget(row, 2, new Label (absentie.getDatum()));
            this.AbsentieFlexTable.setWidget(row, 3, new Label (absentie.getReden()));
            row ++;
            
            
        }
    }

    
    public void service_eventAddAbsentieSuccessful() {
        this.absentieService.listAbsenties();
    }

   
    public void service_eventUpdateAbsentieSuccessful() {
        this.absentieService.listAbsenties();
    }
    
  
    public void service_eventRemoveAbsentieSuccessful() {
        this.absentieService.listAbsenties();
    }

    
    public void service_eventUpdateAbsentieFailed(Throwable caught) {
        Window.alert("Update absentie failed");
    }

    
    public void service_eventAddAbsentieFailed(Throwable caught) {
        Window.alert("Unable to update absentie");
    }

    
    public void service_eventRemoveAbsentieFailed(Throwable caught) {
        Window.alert("Remove absentie failed");
    }

    
    public void service_eventListAbsentiesFailed(Throwable caught) {
       Window.alert("Unable to get absentie list");
        
    }

}
